package com.example.myapplication.tack_30.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_30.Details02Activity;
import com.example.myapplication.tack_30.DetailsActivity;
import com.example.myapplication.tack_30.ThemeActivity;
import com.example.myapplication.tack_30.data.NearData;
import com.example.myapplication.tack_30.data.ProductData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.gson.Gson;

import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.My> {
    private List<ProductData.DataDTO> list;
    private Context context;
    private View view;
    private OnAdd onAdd;
    public ProductAdapter(List<ProductData.DataDTO> list, Context context,OnAdd onAdd) {
        this.list = list;
        this.context = context;
        this.onAdd = onAdd;
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_31_product,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        Glide.with(context).load(OkHttpTool.URL+list.get(position).getImgUrl()).into(holder.imgUrl);

        holder.name.setText(list.get(position).getName());
        holder.saleQuantity.setText("月销量 "+list.get(position).getSaleQuantity());
        holder.favorRate.setText("好评率 "+String.valueOf(list.get(position).getFavorRate()+"%"));
        holder.price1.setText("￥"+String.valueOf(list.get(position).getPrice()));

        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = list.get(position).getNumber();
                number++;
                list.get(position).setNumber(number);

                holder.sum.setText(String.valueOf(number));
                onAdd.clickAdd((float)list.get(position).getPrice());
            }
        });
        holder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = list.get(position).getNumber();
                if(number > 0) {

                    number--;
                    list.get(position).setNumber(number);
                    holder.sum.setText(String.valueOf(number));
                    onAdd.clickMinus((float)list.get(position).getPrice());
                }


            }
        });


    }
    public interface OnAdd{
        void clickAdd(float  price);
        void clickMinus(float price);
    }
    @Override
    public int getItemCount() {
        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView imgUrl;
        private ImageView add;
        private ImageView minus;
        private TextView name;
        private TextView saleQuantity;
        private TextView favorRate;
        private TextView price1;
        private EditText sum;

        public My(@NonNull View itemView) {
            super(itemView);
            imgUrl = view.findViewById(R.id.imgUrl);
            minus = view.findViewById(R.id.minus);
            sum = view.findViewById(R.id.sum);
            name = view.findViewById(R.id.name);
            saleQuantity = view.findViewById(R.id.saleQuantity);
            favorRate = view.findViewById(R.id.favorRate);
            price1 = view.findViewById(R.id.price);
            add = view.findViewById(R.id.add);

        }
    }
}
